﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WoSoft.Camera.ImgEdit
{
    public partial class UserControlPictureBox : UserControl
    {
        public UserControlPictureBox()
        {
            InitializeComponent();
            this.pictureBox.AllowDrop = true;
            this.pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox.DragDrop +=
                new DragEventHandler(PictureBox_DragDrop);
            this.pictureBox.DragEnter +=
                 new DragEventHandler(PictureBox_DragEnter);
        }

        private bool m_dragDropEnable = true;
        private bool m_isOriginalSize = false;
        #region Properties
        public PictureBox PictrueBoxObject
        {
            get { return this.pictureBox; }
        }
        public bool DragDropEnable
        {
            get { return this.m_dragDropEnable; }
            set { m_dragDropEnable = value; }
        }
        public bool IsOriginalSize
        {
            get { return this.m_isOriginalSize; }
            set { m_isOriginalSize = value; }
        }
        #endregion
        #region Methods


        public void DisposeImage()
        {
            if (this.pictureBox.Image != null)
            {
                this.pictureBox.Image.Dispose();
                this.pictureBox.Image = null;
            }
        }
        private Image ZoomImage(Image oldImage)
        {
            if (null == oldImage)
            {
                return null;
            }
            int width = oldImage.Width;
            int height = oldImage.Height;
            int newWidth = 0;
            int newHeight = 0;
            float ratioXY = (float)width / (float)height;
            if ((width > this.pictureBox.Width) ||
                 (height > this.pictureBox.Height))
            {
                if (ratioXY >= 1.0)
                {
                    newWidth = this.pictureBox.Width;
                    newHeight = (int)((float)newWidth / ratioXY) + 1;
                }
                else
                {
                    newHeight = this.pictureBox.Height;
                    newWidth = (int)(newHeight * ratioXY);
                }
                Bitmap bitmap = new Bitmap(newWidth,
                     newHeight, oldImage.PixelFormat);
                Graphics g = Graphics.FromImage(bitmap);
                g.Clear(Color.Transparent);
                g.DrawImage(oldImage,
                    new RectangleF(0, 0, newWidth, newHeight));
                return Image.FromHbitmap(bitmap.GetHbitmap());
            }
            return oldImage;
        }
        #endregion
        #region PictureBox drag and drop events
        private void PictureBox_DragEnter(object sender, DragEventArgs e)
        {
            bool bflag = e.Data.GetDataPresent(DataFormats.FileDrop);
            e.Effect = bflag && this.m_dragDropEnable ? e.Effect =
                DragDropEffects.Copy : DragDropEffects.None;
        }
        private void PictureBox_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                String[] strfileNames =
                    (String[])e.Data.GetData(DataFormats.FileDrop);
                Image dragImage = Image.FromFile(strfileNames[0]);
                if (dragImage != null)
                {
                    this.DisposeImage();
                    this.pictureBox.Image =
                        m_isOriginalSize ? dragImage : ZoomImage(dragImage);
                }
            }
        }
        #endregion
    }
}
